package org.example.system.domain.vo;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import lombok.Data;
import org.example.common.core.utils.StringUtils;
import org.example.common.sensitive.annotation.Sensitive;
import org.example.common.sensitive.core.SensitiveStrategy;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户端信息
 */
@Data
public class SysClientVo implements Serializable {

    private static final long serialVersionUID = -6947822646185526939L;
    /**
     * 客户端ID
     */
    @ExcelProperty(value = "客户端ID")
    @Sensitive(strategy = SensitiveStrategy.CLIENT_ID)
    private String clientId;
    /**
     * 客户端的秘钥密文secret
     */
    @ExcelProperty(value = "秘钥secret")
    @Sensitive(strategy = SensitiveStrategy.CLIENT_SECRET)
    private String clientSecret;
    /**
     * 授权登录方式
     */
    @ExcelProperty(value = "授权登录方式")
    private List<String> grantTypes;
    /**
     * 授权码模式的跳转uri
     */
    @ExcelProperty(value = "授权码模式的跳转uri")
    private String webServerRedirectUri;
    /**
     * 权限,多个用逗号分隔
     */
    @ExcelProperty(value = "权限")
    private String authorities;
    /**
     * access_token的过期时间，单位毫秒，覆盖掉硬编码
     */
    @ExcelProperty(value = "access_token的过期时间")
    private Integer accessTokenValidity;
    /**
     * refresh_token的过期时间，单位毫秒，覆盖掉硬编码
     */
    @ExcelProperty(value = "refresh_token的过期时间")
    private Integer refreshTokenValidity;
    /**
     * The client is trusted or not. If it is trust, will skip approve step
     * default false.
     */
    private boolean trusted = false;
    /**
     * Value is 'true' or 'false',  default 'false'
     */
    @ExcelProperty(value = "是否自动授权")
    private String autoapprove="false";
    /**
     * 描述
     */
    private String remark;
    /**
     * 授权设备
     */
    @ExcelProperty(value = "授权设备")
    private String deviceType;
    /**
     * 授权用户
     */
    @ExcelProperty(value = "授权用户")
    private String grantUser;

    private String status;

    public SysClientVo(){}

    public SysClientVo(SysOauthClientVo sysOauthClientVo){
        BeanUtils.copyProperties(sysOauthClientVo,this);
        this.setClientSecret(sysOauthClientVo.getClientSecretPlaintext());
        this.setGrantTypes(StringUtils.splitList(sysOauthClientVo.getAuthorizedGrantTypes()));
        JSONObject AdditionalInformation=JSON.parseObject(sysOauthClientVo.getAdditionalInformation());
        this.setDeviceType(AdditionalInformation.getString("device_type"));
        this.setGrantUser(AdditionalInformation.getString("grant_user"));
    }

    public static SysClientVo build(SysOauthClientVo sysOauthClientVo){
        if(ObjectUtil.isNull(sysOauthClientVo)) return null;
        return new SysClientVo(sysOauthClientVo);
    }

    public static List<SysClientVo> build(List<SysOauthClientVo> list){
        if(ObjectUtil.isNull(list)) return Lists.newArrayList();
        return list.stream().map(SysClientVo::new).collect(Collectors.toList());
    }
}
